package cn.wanda.bi.sunshine.platform.server.aspect;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.util.CollectionUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import cn.wanda.bi.sunshine.platform.server.SunshineCode;
import cn.wanda.bi.sunshine.platform.server.bean.model.UserLabelAuthAppid;
import cn.wanda.bi.sunshine.platform.server.bean.model.UserLabelAuthAppidExample;
import cn.wanda.bi.sunshine.platform.server.service.UserLabelAuthAppidService;
import cn.wanda.bi.sunshine.platform.server.utils.exception.SunshineException;

/**
 * Created by wangfupeng1 on 2017-5-6.
 */
@Aspect
@Component
@Order(1)
public class AccessLabelAspect {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UserLabelAuthAppidService userLabelAuthAppidService;

    @Before("@annotation(cn.wanda.bi.sunshine.platform.server.aspect.AccessLabel)")
    public void authLabel(JoinPoint point) throws Throwable {
    	String appid = request.getHeader("appid");
    	String secret = request.getHeader("secret");
    	UserLabelAuthAppidExample dimUserLabelAllAuthAppidExample = new UserLabelAuthAppidExample();
    	dimUserLabelAllAuthAppidExample.createCriteria().andAppidEqualTo(appid).andSecretEqualTo(secret);
    	List<UserLabelAuthAppid> dimUserLabelAllAuthAppids= userLabelAuthAppidService.getUserLabelAuthAppids(dimUserLabelAllAuthAppidExample, appid);
    	if(CollectionUtils.isEmpty(dimUserLabelAllAuthAppids))
    		throw new SunshineException(SunshineCode.APPID_WRONG_OR_secret_WRONG);
    }
    
    
}
